var util = require('./utils/util.js');
var api = require('./config/api.js');
var aiChatTools = require('./utils/aiChatTools.js');
var user = require('./utils/user.js');

App({
  onLaunch: function () {
    Promise.prototype.finally = function (callback) {
      let P = this.constructor;
      return this.then(
        value => {
          P.resolve(callback()).then(() => value)
        },
        reason => {
          P.resolve(callback()).then(() => {
            throw reason
          })
        }
      )
    }
    const updateManager = wx.getUpdateManager();
    wx.getUpdateManager().onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    // 获取设备信息
    let that = this
    wx.getSystemInfo({
      success: e => {
        that.globalData.phoneInfo = wx.getSystemInfoSync();
        that.globalData.capsuleButton = wx.getMenuButtonBoundingClientRect();
      }
    })

    //这里重置
    aiChatTools.setConversationId('');

  },
  onShow: function (options) {
    try {
      try {
        user.checkLogin().then(res => {
          this.globalData.hasLogin = true;
        }).catch(() => {
          this.globalData.hasLogin = false;
        });
      } catch (e) {
        console.error(e);
      }

      console.log("---------app onshow ----------------");
      console.log(options);
      //医保支付授权码获取
      if (options.scene) {
        this.globalData.scene = options.scene;
        //医保授权码业务
        if (this.globalData.scene === 1038) {
          let extraData = options.referrerInfo.extraData;
          try {
            if (extraData) {
              if (extraData.retCode == 0) {
                this.globalData.authCode = extraData.authCode;
              } else if (extraData.retCode == 150402) {
                util.showErrorToast("未激活点凭证");
              } else if (extraData.retCode == 150403) {
                util.showErrorToast("未授权获取payAuthNo");
              } else if (extraData.retCode == 150502) {
                util.showErrorToast("所在地区未查询到用户参保信息");
              }
            }
          } catch (e) {
            console.error(e);
          }



        }

      }
    } catch (e) {
      console.error(e);
    }

  },


  globalData: {
    scene: 0,
    hasLogin: false,
    authCode: '',
  }
})